package com.suncnpap.system.service.impl;

import java.util.List;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.app.util.common.Constants;
import com.app.util.database.mybatis.Page;
import com.suncnpap.system.service.AdminService;
import com.suncnpap.system.mapper.AdminMapper;
import com.suncnpap.system.model.Admin;

/** 
 * 说明： 管理员Service实现类
 * @version
 */
@Service("adminService")
public class AdminServiceImpl implements AdminService{

	
	@Autowired
	AdminMapper adminMapper;
	
	@Override
	public void add(Admin obj) {
		// TODO Auto-generated method stub
		adminMapper.add(obj);
	}

	@Override
	public void update(Admin obj) {
		// TODO Auto-generated method stub
		adminMapper.edit(obj);
	}

	@Override
	public void deleteById(String strId) {
		// TODO Auto-generated method stub
		adminMapper.deleteById(strId);
		
	}
	@Override
	public void deleteMore(String[] strIds) {
		// TODO Auto-generated method stub
		adminMapper.deleteMore(strIds);
	}

	@Override
	public Page list(Page page) {
		// TODO Auto-generated method stub
		List<Admin> list = adminMapper.datalistPage(page);
		page.setDataList(list);
		return page;
	}


	@Override
	public int findCount(Page page) {
		// TODO Auto-generated method stub
		return adminMapper.findCount(page);
	}

	@Override
	public Admin findById(String strId) {
		// TODO Auto-generated method stub
		return adminMapper.findById(strId);
	}
	
	/**
	 * 根据用户ID获取用户信息
	 */
	@Override
	public Admin findByUserId(String strUserId, String strSchoolId)  {
		if(Constants.INIT_USER_ID.equals(strUserId)){//初始化管理员登录
			return initRainbowUser();
		}
		return adminMapper.findByUserId(strUserId, strSchoolId);
	}
	
	/**
	 * 构造初始化管理员信息
	 * @return
	 */
	private Admin initRainbowUser(){
		Admin user =new Admin();
		user.setStrUser(Constants.INIT_USER_ID);//管理员ID
		user.setStrSalt(Constants.INIT_USER_SALT);//盐
		user.setStrName(Constants.INIT_USER_NAME);//管理员姓名
		user.setStrSchoolId("-1");
		user.setStrId("0");
		user.setStrPassword(new SimpleHash("MD5", Constants.INIT_USER_PWD, user.getStrSalt()).toString());//密码
		return user;
	}
	
	@Override
	public void updatePassword(String[] strIds, String strPassword, String strSalt) throws Exception{
		// TODO Auto-generated method stub
		adminMapper.updatePassword(strIds, strPassword, strSalt);
	}
}

